﻿<%@ WebHandler Language="C#" Class="MailHandler" %>

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.SessionState;
using ebay.Dal;
using Newtonsoft.Json;

public class MailHandler : IHttpHandler, IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {
        int retVal = 0;
        String retMsg = String.Empty;
        String retSrc = String.Empty;

        if (context.Session["UserId"] == null)
        {
            retVal = 1001;
            retMsg = "您还没有登陆";
        }
        else
        {
            String act = context.Request["act"];
            String id = context.Request["id"];
      
            if (String.IsNullOrEmpty(act) || String.IsNullOrEmpty(id))
            {
                return;
            }
            switch (act)
            {
              #region query
              case "q":
                  {
                    switch (id) {
                      case "0001": {
                          if (context.Session["eBayUserID"] == null) {
                            retVal = 1007;
                            retMsg = "请先选择账户";
                          }
                          else {
                            String eBayUserID = context.Session["eBayUserID"].ToString();
                            String jsonMailInfo = String.Empty;
                            try {
                              String sqlString = ebay.Utility.PubConst.QueryMailCountString;
                              SqlParameter[] parameters = new SqlParameter[] {
                                            new SqlParameter() { ParameterName = "@eBayUserID", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = eBayUserID }
                                        };

                              DataTable dataSource = SqlHelper.ExecuteDataTable(SqlHelper.ceterDB_Conn, CommandType.Text, sqlString, parameters);
                              jsonMailInfo = JsonConvert.SerializeObject(dataSource, new Newtonsoft.Json.Converters.IsoDateTimeConverter());
                            }
                            catch (System.Exception ex) {
                              retVal = 1;
                              retMsg = "数据库查询出错, 错误代码: " + ex.Message + "...";
                            }
                            retSrc = String.Format(
                                                   "{{ \"MailInfo\": {0} }}",
                                                   jsonMailInfo
                                                   );
                          }
                        }
                        break;
                      case "0002": {
                          if (context.Session["eBayUserID"] == null) {
                            retVal = 1;
                            retMsg = "账户为空, 请先选择账户";
                          }
                          else {
                            retSrc = String.Format("{{ \"eBayUserID\": \"{0}\" }}", context.Session["eBayUserID"].ToString());
                          }
                        }
                        break;
                      case "0003": {
                          String eBayUserID = context.Session["eBayUserID"].ToString();
                          String targetID = context.Request["targetid"];
                          String folderID = context.Request["folderid"];
                          int quantity = 0;

                          MailService mail = new MailService();
                          try {
                            switch (targetID) {
                              case "message":
                                quantity = mail.QueryMessageQuantity(eBayUserID, folderID);
                                break;
                              case "dispute":
                                quantity = mail.QueryMessageQuantity(eBayUserID, folderID);
                                break;
                              case "feedback":
                                quantity = mail.QueryMessageQuantity(eBayUserID, folderID);
                                break;
                            }

                            retSrc = String.Format(
                                "{{ \"Quantity\": {0} }}",
                                quantity
                                );
                          }
                          catch (System.Exception ex) {
                            retVal = 1;
                            retMsg = ex.Message.Replace("/r/n", " ");
                          }
                        }
                        break;
                      case "0004": {
                          if (context.Session["eBayUserID"] == null) {
                            retVal = 1007;
                            retMsg = "请先选择账户";
                          }
                          else {
                            String eBayUserID = context.Session["eBayUserID"].ToString();
                            String jsonUserCaseInfo = String.Empty;
                            try {
                              String sqlString = ebay.Utility.PubConst.QueryUserCaseInfoString;
                              SqlParameter[] parameters = new SqlParameter[] {
                                            new SqlParameter() { ParameterName = "@eBayUserID", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = eBayUserID }
                                        };

                              DataTable dataSource = SqlHelper.ExecuteDataTable(SqlHelper.ceterDB_Conn, CommandType.Text, sqlString, parameters);
                              jsonUserCaseInfo = JsonConvert.SerializeObject(dataSource, new Newtonsoft.Json.Converters.IsoDateTimeConverter());
                              retSrc = String.Format(
                                                 "{{ \"UserCaseInfo\": {0} }}",
                                                 jsonUserCaseInfo
                                                 );
                            }
                            catch (System.Exception ex) {
                              retVal = 1;
                              retMsg = "数据库查询出错, 错误代码: " + ex.Message + "...";
                            }
                          }
                        }
                        break;
                      case "0005": {
                          if (context.Session["eBayUserID"] == null) {
                            retVal = 1007;
                            retMsg = "请先选择账户";
                          }
                          else {
                            String eBayUserID = context.Session["eBayUserID"].ToString();
                            String jsonFeedbackInfo = String.Empty;
                            try {
                              String sqlString = ebay.Utility.PubConst.QueryFeedbackInfoString;
                              SqlParameter[] parameters = new SqlParameter[] {
                                            new SqlParameter() { ParameterName = "@eBayUserID", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = eBayUserID }
                                        };

                              DataTable dataSource = SqlHelper.ExecuteDataTable(SqlHelper.ceterDB_Conn, CommandType.Text, sqlString, parameters);
                              jsonFeedbackInfo = JsonConvert.SerializeObject(dataSource, new Newtonsoft.Json.Converters.IsoDateTimeConverter());
                              retSrc = String.Format(
                                                 "{{ \"FeedbackInfo\": {0} }}",
                                                 jsonFeedbackInfo
                                                 );
                            }
                            catch (System.Exception ex) {
                              retVal = 1;
                              retMsg = "数据库查询出错, 错误代码: " + ex.Message + "...";
                            }
                          }
                        }
                        break;
                      //            case "0003":
                      //              {
                      //                String userID = context.Session["UserId"].ToString();
                      //                String jsonUserInfo = String.Empty;
                      //                try
                      //                {
                      //                  String sqlString = @"Select Distinct SysCategory.SysId,SysCategory.SysName,
                      //SubSysCategory.SubSysId,
                      //SubSysCategory.SubSysName,ProgCategory.ProgId,
                      //ProgCategory.ProgName,ProgCategory.ProgExecName,
                      //SysCategory.SortId,SubSysCategory.SortId,ProgCategory.SortId From UserRight
                      //Inner Join ProgCategory On UserRight.ProgId = ProgCategory.ProgId
                      //Inner Join SubSysCategory On ProgCategory.SubSysId = SubSysCategory.SubSysId
                      //Inner Join SysCategory On SubSysCategory.SysId = SysCategory.SysId
                      //WHERE SysName='邮件系统' AND UserID=@UserID
                      //Order By SysCategory.SortId Asc,SubSysCategory.SortId Asc,ProgCategory.SortId Asc";
                      //                  SqlParameter[] parameters = new SqlParameter[] {
                      //                    new SqlParameter() { ParameterName = "@UserID", SqlDbType = SqlDbType.VarChar, Size = 20, Value = userID}
                      //                  };

                      //                  DataTable dataSource = SqlHelper.ExecuteDataTable(SqlHelper.ceterDB_Conn, CommandType.Text, sqlString, parameters);
                      //                  jsonUserInfo = JsonConvert.SerializeObject(dataSource, new Newtonsoft.Json.Converters.IsoDateTimeConverter());
                      //                }
                      //                catch (System.Exception ex)
                      //                {
                      //                  retVal = 1;
                      //                  retMsg = "数据库查询出错, 错误代码: " + ex.Message + "...";
                      //                }

                      //                retSrc = String.Format(
                      //                                       "{{ \"UserInfo\": {0} }}",
                      //                                       jsonUserInfo == String.Empty ? "\"\"" : jsonUserInfo
                      //                                       );
                      //              }
                      //              break;
                      //case "0101":
                      //  {
                      //    String type = context.Request["type"];
                      //    String text = context.Request["text"];

                      //    if (String.IsNullOrEmpty(type) || String.IsNullOrEmpty(text))
                      //    {
                      //      context.Response.Write("");
                      //    }

                      //    Message msg = new Message();
                      //    DataTable table = msg.QueryMessage(type, text, false);
                      //  }
                      //  break;
                      case "0102":
                        break;
                      case "0103": // 归档
                            {
                          String eBayUserID = context.Request["ebayuserid"];
                          String msgID = context.Request["messageid"];

                          try {
                            String sqlString = "UPDATE eBayMessage SET Filing=1,SET [Read]=1 WHERE eBayUserID=@eBayUserID AND MessageID=@MessageID";
                            SqlParameter[] parameters = new SqlParameter[] {
                                        new SqlParameter() { ParameterName = "@eBayUserID", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = eBayUserID },
                                        new SqlParameter() { ParameterName = "@MessageID", SqlDbType = SqlDbType.VarChar, Size = 25, Value = msgID }
                                    };

                            int result = SqlHelper.ExecuteNonQuery(SqlHelper.ceterDB_Conn, CommandType.Text, sqlString, parameters);
                          }
                          catch (System.Exception ex) {
                            retVal = 1;
                            retMsg = "数据库查询出错, 错误代码: " + ex.Message + "...";
                          }
                        }
                            break;
                      case "0104": // 回复
                            break;
                      case "0105": // 升级
                            break;
                      case "0106": // 转发
                            break;
                      case "0108": // 邮件详情
                            {
                              String eBayUserID = context.Request["ebayuserid"];
                              String msgID = context.Request["messageid"];
                              String jsonDetail = String.Empty;

                              try {
                                String sqlString = "SELECT m.Sender,m.SendToName,m.ReceiveDate,m.[Subject],m.[Text] FROM eBayMessage m WHERE eBayUserID=@eBayUserID AND m.MessageID=@MessageID";
                                SqlParameter[] parameters = new SqlParameter[] {
                                        new SqlParameter() { ParameterName = "@eBayUserID", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = eBayUserID },
                                        new SqlParameter() { ParameterName = "@MessageID", SqlDbType = SqlDbType.VarChar, Size = 25, Value = msgID }
                                    };

                                DataTable dataSource = SqlHelper.ExecuteDataTable(SqlHelper.ceterDB_Conn, CommandType.Text, sqlString, parameters);
                                jsonDetail = JsonConvert.SerializeObject(dataSource, new Newtonsoft.Json.Converters.IsoDateTimeConverter());
                              }
                              catch (System.Exception ex) {
                                retVal = 1;
                                retMsg = "数据库查询出错, 错误代码: " + ex.Message + "...";
                              }

                              retSrc = String.Format(
                                                     "{{ \"Detail\": {0} }}",
                                                     jsonDetail
                                                     );
                            }
                            break;
                      case "0109": {
                              String eBayUserID = context.Request["ebayuserid"];
                              String orderID = context.Request["orderid"];
                              String sqlString = @"DECLARE @MsgQuantity INT
DECLARE @Interval INT
SELECT @MsgQuantity=COUNT(m.MessageID),@Interval=DATEDIFF(DAY,MAX(o.PaidonDate),MAX(m.ReceiveDate)) FROM eBayMessage m JOIN eBaySelling o ON m.eBayUserID=o.eBayUserID AND m.Sender=o.UserID WHERE o.eBayUserID=@eBayUserID AND o.OrderID=@OrderID
SELECT TOP 1 DATEADD(DAY,1,o.PaidonDate) AS ShippingDate,p.my_PostageService AS PostageService,@MsgQuantity AS Quantity,o.PaidonDate,d.XDay,d.YDay,d.ZDay FROM eBaySelling o JOIN ParcelPrint p ON o.eBayUserID=p.eBayUserID AND o.OrderLineItemID=p.OrderLineItemID JOIN MessageDeadline d ON p.my_PostageService=d.PostageService WHERE o.eBayUserID=@eBayUserID AND o.OrderId=@OrderID";
                              SqlParameter[] parameters = new SqlParameter[] {
                                    new SqlParameter() { ParameterName = "@eBayUserID", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = eBayUserID  },
                                    new SqlParameter() { ParameterName = "@OrderID", SqlDbType = SqlDbType.NVarChar, Size = 30, Value = orderID }
                                };

                              try {
                                DataTable dataSource = SqlHelper.ExecuteDataTable(SqlHelper.ceterDB_Conn, CommandType.Text, sqlString, parameters);
                                retSrc = String.Format(
                                                       "{{ \"Information\": {0} }}",
                                                       JsonConvert.SerializeObject(dataSource, new Newtonsoft.Json.Converters.IsoDateTimeConverter())
                                                       );
                              }
                              catch (System.Exception ex) {
                                retVal = 1;
                                retMsg = "数据库查询出错, 错误代码: " + ex.Message + "...";
                              }
                            }
                            break;
                      case "0501": {
                              Orders orders = new Orders();
                            }
                            break;
                      case "0201": {
                              String eBayUserID = context.Request["ebayuserid"];
                              String orderID = context.Request["orderid"];
                              Parcel parcel = new Parcel();
                              Orders orders = new Orders();
                              EbaySelling eBaySelling = new EbaySelling();
                              String jsonParcel = String.Empty;
                              String jsonPayPalAddress = String.Empty;
                              String jsoneBayAddress = String.Empty;

                              try {
                                DataTable dataSource = parcel.GetParcelOrder(eBayUserID, orderID);
                                jsonParcel = JsonConvert.SerializeObject(dataSource, new Newtonsoft.Json.Converters.IsoDateTimeConverter());
                              }
                              catch (System.Exception ex) {
                                retVal = 1;
                                retMsg = "数据库查询出错, 错误代码: " + ex.Message + "...";
                                jsonParcel = "";
                              }

                              try {
                                DataTable dataSource = eBaySelling.GeteBayAddress(eBayUserID, orderID);
                                jsoneBayAddress = JsonConvert.SerializeObject(dataSource);
                              }
                              catch (System.Exception ex) {
                                retVal = 1;
                                retMsg = "数据库查询出错, 错误代码: " + ex.Message + "...";
                                jsoneBayAddress = "";
                              }

                              try {
                                DataTable dataSource = orders.GetPayPalAddress(eBayUserID, orderID);
                                jsonPayPalAddress = JsonConvert.SerializeObject(dataSource);
                              }
                              catch (System.Exception ex) {
                                retVal = 1;
                                retMsg = "数据库查询出错, 错误代码: " + ex.Message + "...";
                                jsonPayPalAddress = "";
                              }

                              retSrc = String.Format(
                                                     "{{ \"Parcel\": {0}, \"eBayAddress\": {1}, \"PayPalAddress\": {2} }}",
                                                     jsonParcel,
                                                     jsoneBayAddress,
                                                     jsonPayPalAddress
                                                     );
                            }
                            break;
                      case "0301": {
                              String eBayUserID = context.Request["ebayuserid"];
                              String caseID = context.Request["caseid"];
                              Dispute dispute = new Dispute();
                              String jsonDispute = String.Empty;

                              try {
                                DataTable dataSource = dispute.GetDisputeDetail(eBayUserID, caseID);
                                jsonDispute = JsonConvert.SerializeObject(dataSource, new Newtonsoft.Json.Converters.IsoDateTimeConverter());
                              }
                              catch (System.Exception ex) {
                                retVal = 1;
                                retMsg = "数据库查询出错, 错误代码: " + ex.Message + "...";
                              }

                              retSrc = String.Format(
                                                     "{{ \"Detail\": {0} }}",
                                                     jsonDispute
                                                     );
                            }
                            break;
                      case "0401": {
                              String eBayUserID = context.Request["ebayuserid"];
                              String feedbackID = context.Request["feedbackid"];
                              String jsonFeedback = String.Empty;
                              String jsonReason = String.Empty;

                              if (String.IsNullOrEmpty(eBayUserID) || String.IsNullOrEmpty(feedbackID))
                                break;

                              try {
                                String sqlString = "SELECT DISTINCT o.OrderID,o.UserID,o.SaleDate,o.PaidonDate,p.DispatchDate,f.CommentType,f.CommentTime FROM eBayFeedback f,eBaySelling o,ParcelPrint p WHERE f.eBayUserID=o.eBayUserID and f.OrderLineItemID=o.OrderLineItemID AND o.eBayUserID=p.eBayUserID AND o.OrderLineItemID=p.OrderLineItemID AND p.Printed='y' AND f.eBayUserID=@eBayUserID and f.FeedbackID=@FeedbackID";

                                SqlParameter[] parameters = new SqlParameter[] {
                                    new SqlParameter() { ParameterName = "@eBayUserID", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = eBayUserID },
                                    new SqlParameter() { ParameterName = "@FeedbackID", SqlDbType = SqlDbType.VarChar, Size = 20, Value = feedbackID }
                                };

                                DataTable tableSource = SqlHelper.ExecuteDataTable(SqlHelper.ceterDB_Conn, CommandType.Text, sqlString, parameters);
                                jsonFeedback = JsonConvert.SerializeObject(tableSource, new Newtonsoft.Json.Converters.IsoDateTimeConverter());

                                sqlString = "SELECT Reason FROM eBayFeedbackReason r WHERE r.eBayUserID=@eBayUserID and r.FeedbackID=@FeedbackID";
                                tableSource = SqlHelper.ExecuteDataTable(SqlHelper.ceterDB_Conn, CommandType.Text, sqlString, parameters);
                                jsonReason = JsonConvert.SerializeObject(tableSource, new Newtonsoft.Json.Converters.IsoDateTimeConverter());

                                retSrc = String.Format(
                                                     "{{ \"Feedback\": {0}, \"Reasons\": {1} }}",
                                                     jsonFeedback,
                                                     jsonReason
                                                     );

                              }
                              catch (System.Exception ex) {
                                retVal = 1;
                                retMsg = "数据库查询出错, 错误代码: " + ex.Message + "...";
                              }


                            }
                            break;
                      case "0601": {
                              String eBayUserID = context.Request["ebayuserid"];
                              String orderID = context.Request["orderid"];
                              String jsonAddress = String.Empty;

                              if (String.IsNullOrEmpty(eBayUserID) || String.IsNullOrEmpty(orderID)) {
                                retVal = 1;
                                retMsg = "查询出错, 错误代码: eBayUserID或orderID不能为空...";
                              }

                              try {
                                String sqlString = @"IF NOT EXISTS (SELECT 1 FROM ParcelPrint p WHERE p.eBayUserID=@eBayUserID AND p.OrderID=@OrderID)
BEGIN
  SELECT DISTINCT o.BuyerName AS Name,o.AddressLine1+' '+o.AddressLine2 AS Street,o.City,o.County,o.Country,o.PostalCode AS ZipCode,o.BuyerPhoneNumber AS Telephone,o.BuyerEmail AS Email
      ,o.BuyerFullName AS eBayName,o.BuyerAddress1+' '+o.BuyerAddress2 AS eBayStreet,o.BuyerCity AS eBayCity,o.BuyerCounty AS eBayCounty,o.BuyerCountry AS eBayCountry,o.BuyerPostcode AS eBayZipCode
      ,o.PostageService,NULL AS Company,NULL AS MyShippingType
    FROM Orders o WHERE o.eBayUserID=@eBayUserID AND o.OrderID=@OrderID
END
ELSE
BEGIN
  SELECT DISTINCT p.BuyerName AS Name,p.AddressLine1+' '+p.AddressLine2 AS Street,p.City,p.County,p.Country,p.PostalCode AS ZipCode,p.BuyerPhoneNumber AS Telephone,p.BuyerEmail AS Email
      ,p.BuyerFullName AS eBayName,p.BuyerAddress1+' '+p.BuyerAddress2 AS eBayStreet,p.BuyerCity AS eBayCity,p.BuyerCounty AS eBayCounty,p.BuyerCountry AS eBayCountry,p.BuyerPostcode AS eBayZipCode
      ,p.PostageService,c.FName AS Company,u.FSourceName AS MyShippingType
    FROM ParcelPrint p JOIN [192.168.1.220].[MMS].[dbo].[ParcelPrintCarriageUsedService] u ON p.my_PostageService=u.FName JOIN [192.168.1.220].[MMS].[dbo].[CarriageService] s ON u.FServiceID=s.FID JOIN [192.168.1.220].[MMS].[dbo].[CarriageCompany] c ON s.FCarriageID=c.FID
    WHERE p.eBayUserID=@eBayUserID AND p.OrderID=@OrderID
END";
                                SqlParameter[] parameters = new SqlParameter[] {
                                        new SqlParameter() { ParameterName = "@eBayUserID", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = eBayUserID },
                                        new SqlParameter() { ParameterName = "@OrderID", SqlDbType = SqlDbType.NVarChar, Size = 30, Value = orderID }
                                    };
                                DataTable tableSource = SqlHelper.ExecuteDataTable(SqlHelper.ceterDB_Conn, CommandType.Text, sqlString, parameters);
                                jsonAddress = JsonConvert.SerializeObject(tableSource, new Newtonsoft.Json.Converters.IsoDateTimeConverter());
                              }
                              catch (System.Exception ex) {
                                retVal = 1;
                                retMsg = "数据库查询出错, 错误代码: " + ex.Message.Replace("\r\n", " ") + "...";
                              }
                              retSrc = String.Format(
                                                     "{{ \"Information\": {0} }}",
                                                     jsonAddress
                                                     );
                            }
                            break;
                      case "6002": {
                              String eBayUserID = context.Request["ebayuserid"];
                              String orderID = context.Request["orderid"];

                              if (String.IsNullOrEmpty(eBayUserID) || String.IsNullOrEmpty(orderID)) {
                                retVal = 1;
                                retMsg = "查询出错, 错误代码: eBayUserID或orderID不能为空...";
                              }

                              try {
                                String sqlString = @"IF NOT EXISTS (SELECT 1 FROM ParcelPrint p WHERE p.eBayUserID=@eBayUserID AND p.OrderID=@OrderID)
BEGIN
  SELECT o.BuyerName AS Name,o.AddressLine1+' '+o.AddressLine2 AS Street,o.City,o.County,o.Country,o.PostalCode AS ZipCode,o.BuyerPhoneNumber AS Telephone,o.BuyerEmail AS Email
      ,o.BuyerFullName AS eBayName,o.BuyerAddress1+' '+o.BuyerAddress2 AS eBayStreet,o.BuyerCity AS eBayCity,o.BuyerCounty AS eBayCounty,o.BuyerCountry AS eBayCountry,o.BuyerPostcode AS eBayZipCode
      ,o.PostageService,NULL AS Company,NULL AS MyShippingType
    FROM Orders o WHERE o.eBayUserID=@eBayUserID AND o.OrderID=@OrderID
END
ELSE
BEGIN
  SELECT p.BuyerName AS Name,p.AddressLine1+' '+p.AddressLine2 AS Street,p.City,p.County,p.Country,p.PostalCode AS ZipCode,p.BuyerPhoneNumber AS Telephone,p.BuyerEmail AS Email
      ,p.BuyerFullName AS eBayName,p.BuyerAddress1+' '+p.BuyerAddress2 AS eBayStreet,p.BuyerCity AS eBayCity,p.BuyerCounty AS eBayCounty,p.BuyerCountry AS eBayCountry,p.BuyerPostcode AS eBayZipCode
      ,p.PostageService,c.FName AS Company,u.FSourceName AS MyShippingType
    FROM ParcelPrint p JOIN [192.168.1.220].[MMS].[dbo].[CarriageUsedService] u ON p.my_PostageService=u.FName JOIN [192.168.1.220].[MMS].[dbo].[CarriageService] s ON u.FServiceID=s.FID JOIN [192.168.1.220].[MMS].[dbo].[CarriageCompany] c ON s.FCarriageID=c.FID
    WHERE p.eBayUserID=@eBayUserID AND p.OrderID=@OrderID
END";
                                SqlParameter[] parameters = new SqlParameter[] {
                                        new SqlParameter() { ParameterName = "@eBayUserID", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = eBayUserID },
                                        new SqlParameter() { ParameterName = "@OrderID", SqlDbType = SqlDbType.NVarChar, Size = 30, Value = orderID }
                                    };
                                DataTable tableSource = SqlHelper.ExecuteDataTable(SqlHelper.ceterDB_Conn, CommandType.Text, sqlString, parameters);
                                String jsonAddress = JsonConvert.SerializeObject(tableSource, new Newtonsoft.Json.Converters.IsoDateTimeConverter());

                                retSrc = String.Format(
                                                       "{{ \"Information\": {0} }}",
                                                       jsonAddress
                                                       );
                              }
                              catch (System.Exception ex) {
                                retVal = 1;
                                retMsg = "数据库查询出错, 错误代码: " + ex.Message.Replace("\r\n", " ") + "...";
                              }
                            }
                            break;
                      //case "6003":
                      //    {
                      //        String templateID = context.Request["templateid"];
                      //        String sqlString = "SELECT Text AS Content FROM MessageTemplate WHERE ID=@TemplateID";
                      //        SqlParameter[] parameters = new SqlParameter[] {
                      //            new SqlParameter() { ParameterName = "@TemplateID", SqlDbType = SqlDbType.Int, Value = templateID }
                      //        };
                      //        try {
                      //            DataTable tableSource = SqlHelper.ExecuteDataTable(SqlHelper.ceterDB_Conn, CommandType.Text, sqlString, parameters);
                      //            String jsonTemplate = JsonConvert.SerializeObject(tableSource, new Newtonsoft.Json.Converters.IsoDateTimeConverter());

                      //            retSrc = String.Format(
                      //                                   "{{ \"Templates\": {0} }}",
                      //                                   jsonTemplate
                      //                                   );
                      //        }
                      //        catch (System.Exception ex)
                      //        {
                      //            retVal = 1;
                      //            retMsg = "数据库查询出错, 错误代码: " + ex.Message.Replace("\r\n", " ") + "...";
                      //        }
                      //    }
                      //  break;
                      case "1001": {
                              String addr = context.Request["addr"];
                              String email = context.Request["email"];
                              String days = context.Request["days"];

                              if (String.IsNullOrEmpty(addr)) {
                                retVal = 1;
                                retMsg = "保存设置出错, 错误代码: 退回地址不能为空...";
                              }
                              else if (String.IsNullOrEmpty(email)) {
                                retVal = 1;
                                retMsg = "保存设置出错, 错误代码: 回复邮箱不能为空...";
                              }
                              else if (String.IsNullOrEmpty(days)) {
                                retVal = 1;
                                retMsg = "保存设置出错, 错误代码: 未关闭付款天数不能为空...";
                              }
                              else {
                                try {
                                  System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
                                  config.AppSettings.Settings["EmailSystem.Address"].Value = addr;
                                  config.AppSettings.Settings["EmailSystem.Email"].Value = email;
                                  config.AppSettings.Settings["EmailSystem.Days"].Value = days;
                                  config.Save();
                                }
                                catch (System.Exception ex) {
                                  retVal = 1;
                                  retMsg = "保存设置出错, 错误代码: " + ex.Message.Replace("\r\n", " ") + "...";
                                }
                              }
                            }
                            break;
                      case "6003": {
                              String eBayUserID = context.Request["eBayUserID"];
                              String orderID = context.Request["OrderID"];
                              String content = context.Request["Content"];
                              ebay.BLL.Mail.TemplateManage manage = new ebay.BLL.Mail.TemplateManage(eBayUserID, orderID);
                              String replaceContent = manage.AutoReplaceWords(content);

                              retSrc = String.Format(
                                                     "{{ \"Message\": \"{0}\" }}",
                                                     replaceContent
                                                     );
                              //ebay.Dal.Mail.TemplateService service = new ebay.Dal.Mail.TemplateService();

                              //      if (service.Initialize(eBayUserID, orderID)) {
                              //        content = service.GetString(content);

                              //        retSrc = String.Format(
                              //          "{{ \"Message\": {0} }}",
                              //          JsonConvert.ToString(content)
                              //        );
                              //      }
                              //      else {
                              //        retVal = 1;
                              //        retMsg = "初始化模板出错...";
                              //      }

                              //    }
                              //    break;
                            }
                            break;
                    }
                  }
                  break;
#endregion // query
              case "l":
                  {
                      switch (id)
                      {
                        case "0002":
                            {
                                String jsonMailInfo = String.Empty;
                                try
                                {
                                    String sqlString = "SELECT eBayUserID FROM eBayAccount a";

                                    DataTable dataSource = SqlHelper.ExecuteDataTable(SqlHelper.ceterDB_Conn, CommandType.Text, sqlString, new SqlParameter[0]);
                                    jsonMailInfo = JsonConvert.SerializeObject(dataSource, new Newtonsoft.Json.Converters.IsoDateTimeConverter());
                                }
                                catch (System.Exception ex)
                                {
                                    retVal = 1;
                                    retMsg = "数据库查询出错, 错误代码: " + ex.Message + "...";
                                }
                                retSrc = String.Format(
                                                       "{{ \"MailInfo\": {0} }}",
                                                       jsonMailInfo
                                                       );
                            }
                            break;
                      }
                  }
                  break;
                    #region update
              case "u":
                  {
                    switch (id) {
                      // 归档
                      case "0103": {
                          String eBayUserID = context.Request["ebayuserid"];
                          String messageID = context.Request["messageid"];
                          try {
                            String sqlString = "UPDATE eBayMessage SET [File]=1 WHERE eBayUserID=@eBayUserID AND MessageID=@MessageID";
                            SqlParameter[] parameters = new SqlParameter[] {
                                        new SqlParameter() { ParameterName = "@eBayUserID", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = eBayUserID },
                                        new SqlParameter() { ParameterName = "@MessageID", SqlDbType = SqlDbType.VarChar, Size = 25, Value = messageID }
                                    };

                            int result = SqlHelper.ExecuteNonQuery(SqlHelper.ceterDB_Conn, CommandType.Text, sqlString, parameters);
                          }
                          catch (System.Exception ex) {
                            retVal = 1;
                            retMsg = "数据库执行出错, 错误代码: " + ex.Message.Replace("\r\n", " ") + "...";
                          }
                        }
                        break;
                      case "0104": {
                          String eBayUserID = context.Request["ebayuserid"];
                          String messageID = context.Request["messageid"];
                          try {
                            String sqlString = "UPDATE eBayMessage SET [Upgrade]=1, TreeID=7 WHERE eBayUserID=@eBayUserID AND MessageID=@MessageID";
                            SqlParameter[] parameters = new SqlParameter[] {
                                        new SqlParameter() { ParameterName = "@eBayUserID", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = eBayUserID },
                                        new SqlParameter() { ParameterName = "@MessageID", SqlDbType = SqlDbType.VarChar, Size = 25, Value = messageID }
                                    };

                            int result = SqlHelper.ExecuteNonQuery(SqlHelper.ceterDB_Conn, CommandType.Text, sqlString, parameters);
                          }
                          catch (System.Exception ex) {
                            retVal = 1;
                            retMsg = "数据库执行出错, 错误代码: " + ex.Message.Replace("\r\n", " ") + "...";
                          }
                        }
                        break;
                      // 移动Message位置
                      case "0105": {
                          String eBayUserID = context.Request["ebayuserid"];
                          String messageID = context.Request["messageid"];
                          String treeID = context.Request["treeid"];
                          try {
                            String sqlString = "UPDATE eBayMessage SET TreeID=@TreeID WHERE eBayUserID=@eBayUserID AND MessageID=@MessageID";

                            SqlParameter[] parameters = new SqlParameter[] {
                                        new SqlParameter() { ParameterName = "@eBayUserID", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = eBayUserID },
                                        new SqlParameter() { ParameterName = "@MessageID", SqlDbType = SqlDbType.VarChar, Size = 25, Value = messageID },
                                        new SqlParameter() { ParameterName = "@TreeID", SqlDbType = SqlDbType.Int, Value = treeID }
                                    };

                            int result = SqlHelper.ExecuteNonQuery(SqlHelper.ceterDB_Conn, CommandType.Text, sqlString, parameters);
                          }
                          catch (System.Exception ex) {
                            retVal = 1;
                            retMsg = "数据库执行出错, 错误代码: " + ex.Message.Replace("\r\n", " ") + "...";
                          }
                        }
                        break;
                      case "moveresponse":
                        {
                          String eBayUserID = context.Request["ebayuserid"];
                          String messageID = context.Request["messageid"];
                          String treeID = context.Request["treeid"];

                          try {
                            String sqlString = "UPDATE OigMessage SET TreeID=@TreeID WHERE eBayUserID=@eBayUserID AND MessageID=@MessageID";

                            SqlParameter[] parameters = new SqlParameter[] {
                                        new SqlParameter() { ParameterName = "@eBayUserID", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = eBayUserID },
                                        new SqlParameter() { ParameterName = "@MessageID", SqlDbType = SqlDbType.VarChar, Size = 25, Value = messageID },
                                        new SqlParameter() { ParameterName = "@TreeID", SqlDbType = SqlDbType.Int, Value = treeID }
                                    };

                            int result = SqlHelper.ExecuteNonQuery(SqlHelper.ceterDB_Conn, CommandType.Text, sqlString, parameters);
                          }
                          catch (System.Exception ex) {
                            retVal = 1;
                            retMsg = "数据库执行出错, 错误代码: " + ex.Message.Replace("\r\n", " ") + "...";
                          }
                        }
                        break;
                      case "0106": {
                          String eBayUserID = context.Request["ebayuserid"];
                          String messageID = context.Request["messageid"];
                          String flag = context.Request["flag"];

                          int iFlag = -1;

                          switch (flag) {
                            case "gray":
                              iFlag = 0;
                              break;
                            case "blue":
                              iFlag = 1;
                              break;
                            case "green":
                              iFlag = 2;
                              break;
                            case "orange":
                              iFlag = 3;
                              break;
                            case "purple":
                              iFlag = 4;
                              break;
                            case "red":
                              iFlag = 5;
                              break;
                            case "yellow":
                              iFlag = 6;
                              break;
                            default:
                              break;
                          }

                          if (iFlag == -1) {
                            retVal = 1;
                            retMsg = "所选择旗帜不存在...";
                            break;
                          }

                          try {
                            String sqlString = "UPDATE eBayMessage SET Flag=@Flag WHERE eBayUserID=@eBayUserID AND MessageID=@MessageID";

                            SqlParameter[] parameters = new SqlParameter[] {
                                        new SqlParameter() { ParameterName = "@eBayUserID", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = eBayUserID },
                                        new SqlParameter() { ParameterName = "@MessageID", SqlDbType = SqlDbType.VarChar, Size = 25, Value = messageID },
                                        new SqlParameter() { ParameterName = "@Flag", SqlDbType = SqlDbType.Int, Value = iFlag }
                                    };

                            int result = SqlHelper.ExecuteNonQuery(SqlHelper.ceterDB_Conn, CommandType.Text, sqlString, parameters);
                          }
                          catch (System.Exception ex) {
                            retVal = 1;
                            retMsg = "数据库执行出错, 错误代码: " + ex.Message.Replace("\r\n", " ") + "...";
                          }
                        }
                        break;
                      // mark message to read
                      case "0107": {
                          String eBayUserID = context.Request["ebayuserid"];
                          String messageID = context.Request["messageid"];
                          ebay.BLL.Mail.MessageManage manage = new ebay.BLL.Mail.MessageManage();
                        
                          try {
                            manage.MarkMessageRead(eBayUserID, messageID);
                          }
                          catch (System.Exception ex) {
                            retVal = 1;
                            retMsg = "数据库执行出错, 错误代码: " + ex.Message.Replace("\r\n", " ") + "...";
                          }
                          //try {
                          //  String sqlString = "UPDATE eBayMessage SET [Read]=1 WHERE eBayUserID=@eBayUserID AND MessageID=@MessageID";

                          //  SqlParameter[] parameters = new SqlParameter[] {
                          //              new SqlParameter() { ParameterName = "@eBayUserID", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = eBayUserID },
                          //              new SqlParameter() { ParameterName = "@MessageID", SqlDbType = SqlDbType.VarChar, Size = 25, Value = messageID },
                          //          };

                          //  int result = SqlHelper.ExecuteNonQuery(SqlHelper.ceterDB_Conn, CommandType.Text, sqlString, parameters);
                          //}
                          //catch (System.Exception ex) {
                          //  retVal = 1;
                          //  retMsg = "数据库执行出错, 错误代码: " + ex.Message.Replace("\r\n", " ") + "...";
                          //}
                        }
                        break;
                      // Update Message's ordersalesrecordnumber
                      case "0108": {
                          String eBayUserID = context.Request["ebayuserid"];
                          String msgID = context.Request["messageid"];
                          String orderSalesRecordNumber = context.Request["ordersalesrecordernumber"];
                          String sqlString = "UPDATE eBayMessage SET OrderSalesRecordNumber=@OrderSalesRecordNumber WHERE eBayUserID=@eBayUserID AND MessageID=@MessageID";
                          SqlParameter[] parameters = new SqlParameter[] {
                                    new SqlParameter() { ParameterName = "@eBayUserID", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = eBayUserID },
                                    new SqlParameter() { ParameterName = "@MessageID", SqlDbType = SqlDbType.VarChar, Size = 25, Value = msgID },
                                    new SqlParameter() { ParameterName = "@OrderSalesRecordNumber", SqlDbType = SqlDbType.NVarChar, Size = 30, Value = orderSalesRecordNumber }
                                };
                          try {
                            int result = SqlHelper.ExecuteNonQuery(SqlHelper.ceterDB_Conn, CommandType.Text, sqlString, parameters);
                          }
                          catch (System.Exception ex) {
                            retVal = 1;
                            retMsg = "数据库执行出错, 错误代码: " + ex.Message.Replace("\r\n", " ") + "...";
                          }
                        }
                        break;
                      case "0109": {
                          String eBayUserID = context.Request["ebayuserid"];
                          String orderID = context.Request["orderid"];
                          String name = context.Request["name"];
                          String company = context.Request["company"];
                          String street = context.Request["street"];
                          String city = context.Request["city"];
                          String county = context.Request["county"];
                          String country = context.Request["country"];
                          String zipCode = context.Request["zipcode"];
                          String telephone = context.Request["telephone"];
                          String email = context.Request["email"];
                          String postageService = context.Request["postageservice"];

                          String sqlString = "SELECT 1 FROM ParcelPrint p WHERE p.eBayUserID=@eBayUserID AND p.orderID=@OrderID";
                          try {
                            DataTable ppTable = SqlHelper.ExecuteDataTable(SqlHelper.ceterDB_Conn, CommandType.Text, sqlString, new SqlParameter[0]);

                            if (ppTable.Rows.Count == 0) {
                              sqlString = "UPDATE Orders SET BuyerName=@BuyerName,PostageService=@PostageService,AddressLine1=@Street,AddressLine2='',City=@City,County=@County,Country=@Country,PostalCode=@ZipCode,BuyerPhoneNumber=@Telephone,BuyerEmail=@Email WHERE eBayUserID=@eBayUserID AND orderID=@OrderID";

                              SqlParameter[] parameters = new SqlParameter[] {
                                            new SqlParameter() { ParameterName = "@BuyerName", SqlDbType = SqlDbType.NVarChar, Size = 255, Value = name },
                                            new SqlParameter() { ParameterName = "@PostageService", SqlDbType = SqlDbType.NVarChar, Size = 60, Value = postageService },
                                            new SqlParameter() { ParameterName = "@Street", SqlDbType = SqlDbType.NVarChar, Size = 255, Value = street },
                                            new SqlParameter() { ParameterName = "@City", SqlDbType = SqlDbType.NVarChar, Size = 60, Value = city },
                                            new SqlParameter() { ParameterName = "@County", SqlDbType = SqlDbType.NVarChar, Size = 255, Value = county },
                                            new SqlParameter() { ParameterName = "@Country", SqlDbType = SqlDbType.NVarChar, Size = 60, Value = country },
                                            new SqlParameter() { ParameterName = "@ZipCode", SqlDbType = SqlDbType.NVarChar, Size = 30, Value = zipCode },
                                            new SqlParameter() { ParameterName = "@Telephone", SqlDbType = SqlDbType.NVarChar, Size = 50, Value = telephone },
                                            new SqlParameter() { ParameterName = "@Email", SqlDbType = SqlDbType.NVarChar, Size = 255, Value = email },
                                        };

                              int result = SqlHelper.ExecuteNonQuery(SqlHelper.ceterDB_Conn, CommandType.Text, sqlString, parameters);
                            }
                            else {
                              retVal = 2;
                              retMsg = "订单已打印, 不能修改...";
                            }
                          }
                          catch (System.Exception ex) {
                            retVal = 1;
                            retMsg = "数据库执行出错, 错误代码: " + ex.Message.Replace("\r\n", " ") + "...";
                          }
                        }
                        break;
                      // 评价拒绝撤销
                      case "0110": {
                          String eBayUserID = context.Request["ebayuserid"];
                          String feedbackID = context.Request["feedbackid"];
                          ebay.BLL.FeedbackManage manage = new ebay.BLL.FeedbackManage();
                         
                          try {
                            manage.SetFeedbackReject(eBayUserID, feedbackID);
                          }
                          catch (System.Exception ex) {
                            retVal = 1;
                            retMsg = "数据库执行出错, 错误代码: " + ex.Message.Replace("\r\n", " ") + "...";
                          }
                        }
                        break;
                      // 发送撤销请求
                      case "cancelfeedback":
                        {
                          String eBayUserID = context.Request["ebayuserid"];
                          String feedbackID = context.Request["feedbackid"];
                          
                          try {
                            
                          }
                          catch (System.Exception ex) {
                            retVal = 1;
                            retMsg = "数据库执行出错, 错误代码: " + ex.Message.Replace("\r\n", " ") + "...";
                          }
                        }
                        break;
                      // Update message flag
                      case "0111": {
                          String eBayUserID = context.Request["eBayUserID"];
                          String name = context.Request["Name"];
                          String jsonMsgID = context.Request["MessageIDs"];
                          String[] msgIDs = JsonConvert.DeserializeObject<List<String>>(jsonMsgID).ToArray();

                          using (SqlConnection conn = new SqlConnection(SqlHelper.ceterDB_Conn)) {
                            try {
                              conn.Open();
                            }
                            catch (System.Exception ex) {
                              retVal = 1;
                              retMsg = "连接数据库出错, 错误代码: " + ex.Message.Replace("\r\n", " ") + "...";
                              Log.WriteLog(ex.Message);
                            }

                            using (SqlTransaction tran = conn.BeginTransaction()) {
                              foreach (String msgID in msgIDs) {
                                String sqlString = "UPDATE eBayMessage SET Category=f.Usercode FROM MenuFlag f WHERE eBayUserID=@eBayUserID AND MessageID=@MessageID AND f.Usercode=@Name";

                                SqlParameter[] parameters = new SqlParameter[] {
                                                new SqlParameter() { ParameterName = "@eBayUserID", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = eBayUserID },
                                                new SqlParameter() { ParameterName = "@MessageID", SqlDbType = SqlDbType.VarChar, Size = 25, Value = msgID },
                                                new SqlParameter() { ParameterName = "@Name ", SqlDbType = SqlDbType.NVarChar, Size = 255, Value = name }
                                            };
                                SqlHelper.ExecuteNonQuery(tran, CommandType.Text, sqlString, parameters);
                              }

                              try {
                                tran.Commit();
                              }
                              catch (System.Exception ex) {
                                tran.Rollback();
                                retVal = 1;
                                retMsg = "数据库执行出错, 错误代码: " + ex.Message.Replace("\r\n", " ") + "...";
                                Log.WriteLog(ex.Message);
                              }
                            }
                          }
                        }
                        break;
                      case "0112": {
                          String eBayUserID = context.Request["eBayUserID"];
                          String name = context.Request["Name"];
                          String jsonResponseIDs = context.Request["IDs"];
                          String[] msgIDs = JsonConvert.DeserializeObject<List<String>>(jsonResponseIDs).ToArray();

                          using (SqlConnection conn = new SqlConnection(SqlHelper.ceterDB_Conn)) {
                            try {
                              conn.Open();
                            }
                            catch (System.Exception ex) {
                              retVal = 1;
                              retMsg = "连接数据库出错, 错误代码: " + ex.Message.Replace("\r\n", " ") + "...";
                              Log.WriteLog(ex.Message);
                            }

                            using (SqlTransaction tran = conn.BeginTransaction()) {
                              foreach (String msgID in msgIDs) {
                                String sqlString = "UPDATE OigMessage SET Category=f.Usercode FROM MenuFlag f WHERE eBayUserID=@eBayUserID AND MessageID=@MessageID AND f.Usercode=@Name";
                                SqlParameter[] parameters = new SqlParameter[] {
                                                new SqlParameter() { ParameterName = "@eBayUserID", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = eBayUserID },
                                                new SqlParameter() { ParameterName = "@MessageID", SqlDbType = SqlDbType.VarChar, Size = 25, Value = msgID },
                                                new SqlParameter() { ParameterName = "@Name", SqlDbType = SqlDbType.NVarChar, Size = 255, Value = name }
                                            };
                                SqlHelper.ExecuteNonQuery(tran, CommandType.Text, sqlString, parameters);
                              }

                              try {
                                tran.Commit();
                              }
                              catch (System.Exception ex) {
                                tran.Rollback();
                                retVal = 1;
                                retMsg = "数据库执行出错, 错误代码: " + ex.Message.Replace("\r\n", " ") + "...";
                                Log.WriteLog(ex.Message);
                              }
                            }
                          }
                        }
                        break;
                      // response file
                      case "0113": {
                          String eBayUserID = context.Request["ebayuserid"];
                          String msgID = context.Request["messageid"];

                          try {
                            String sqlString = "UPDATE OigMessage SET Filing=1 WHERE eBayUserID=@eBayUserID AND MessageID=@MessageID";
                            SqlParameter[] parameters = new SqlParameter[] {
                                        new SqlParameter() { ParameterName = "@eBayUserID", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = eBayUserID },
                                        new SqlParameter() { ParameterName = "@MessageID", SqlDbType = SqlDbType.VarChar, Size = 25, Value = msgID }
                                    };

                            int result = SqlHelper.ExecuteNonQuery(SqlHelper.ceterDB_Conn, CommandType.Text, sqlString, parameters);
                          }
                          catch (System.Exception ex) {
                            retVal = 1;
                            retMsg = "数据库查询出错, 错误代码: " + ex.Message + "...";
                          }
                        }
                        break;
                      // response update
                      case "0114": {

                        }
                        break;
                      case "feedbackresponse":
                        {
                          String eBayUserID = context.Request["eBayUserID"];
                          String feedbackID = context.Request["FeedbackID"];
                          String body = context.Request["Body"];
                          ebay.BLL.FeedbackManage manage = new ebay.BLL.FeedbackManage();

                          manage.SaveResponse(eBayUserID, feedbackID, body);
                        }
                        break;
                      // leave a message
                      case "0115": {
                          String eBayUserID = context.Request["eBayUserID"];
                          String feedbackID = context.Request["FeedbackID"];
                          String body = context.Request["Body"];
                          String subject = "Regarding the feedback you left";

                          try {
                            String sqlString = @"INSERT OigMessage(eBayUserID,RecipientID,[Subject],Body,FolderID,[Type],ImportDate)
  SELECT DISTINCT @eBayUserID,f.CommentingUser,@Subject,@Body,8704,'AAQ',@ImportDate FROM eBayFeedback f WHERE f.eBayUserID=@eBayUserID AND f.FeedbackID=@FeedbackID
INSERT OigResponse(eBayUserID,FeedbackID,ItemID,OrderLineItemID,ResponseText,TargetUserID,FolderID,ImportDate)
  SELECT DISTINCT @eBayUserID,@FeedbackID,f.ItemID,f.OrderLineItemID,@Body,f.CommentingUser,8704,@ImportDate FROM eBayFeedback f WHERE f.eBayUserID=@eBayUserID AND f.FeedbackID=@FeedbackID";
                            SqlParameter[] parameters = new SqlParameter[] {
                                        new SqlParameter() { ParameterName = "@eBayUserID", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = eBayUserID },
                                        new SqlParameter() { ParameterName = "@FeedbackID", SqlDbType = SqlDbType.VarChar, Size = 20, Value = feedbackID },
                                        new SqlParameter() { ParameterName = "@Body", SqlDbType = SqlDbType.NVarChar, Value = body },
                                        new SqlParameter() { ParameterName = "@Subject", SqlDbType = SqlDbType.NVarChar, Size = 255, Value = subject },
                                        new SqlParameter() { ParameterName = "@ImportDate", SqlDbType = SqlDbType.DateTime, Value = DateTime.Now }
                                    };

                            int result = SqlHelper.ExecuteNonQuery(SqlHelper.ceterDB_Conn, CommandType.Text, sqlString, parameters);
                          }
                          catch (System.Exception ex) {
                            retVal = 1;
                            retMsg = "数据库查询出错, 错误代码: " + ex.Message + "...";
                          }
                        }
                        break;
                      // response to feedback
                      case "8016": {
                          String eBayUserID = context.Request["eBayUserID"];
                          String feedbackID = context.Request["FeedbackID"];
                          String body = context.Request["Body"];

                          try {
                            String sqlString = @"INSERT OigResponse(eBayUserID,FeedbackID,ItemID,OrderLineItemID,ResponseText,TargetUserID,FolderID,ImportDate)
  SELECT DISTINCT @eBayUserID,@FeedbackID,f.ItemID,f.OrderLineItemID,@Body,f.CommentingUser,8704,@ImportDate FROM eBayFeedback f WHERE f.eBayUserID=@eBayUserID AND f.FeedbackID=@FeedbackID";
                            SqlParameter[] parameters = new SqlParameter[] {
                                        new SqlParameter() { ParameterName = "@eBayUserID", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = eBayUserID },
                                        new SqlParameter() { ParameterName = "@FeedbackID", SqlDbType = SqlDbType.VarChar, Size = 20, Value = feedbackID },
                                        new SqlParameter() { ParameterName = "@Body", SqlDbType = SqlDbType.NVarChar, Value = body }
                                    };

                            int result = SqlHelper.ExecuteNonQuery(SqlHelper.ceterDB_Conn, CommandType.Text, sqlString, parameters);
                          }
                          catch (System.Exception ex) {
                            retVal = 1;
                            retMsg = "数据库查询出错, 错误代码: " + ex.Message + "...";
                          }
                        }
                        break;
                      case "0116": {
                          String templateID = context.Request["templateid"];
                          String content = context.Request["content"];
                          String comment = context.Request["comment"];

                          try {
                            String sqlString = @"UPDATE MessageTemplate SET Text=@Text,Comment=@Comment WHERE ID=@ID";
                            SqlParameter[] parameters = new SqlParameter[] {
                                          new SqlParameter() { ParameterName = "@ID", SqlDbType = SqlDbType.NVarChar, Value = templateID },
                                          new SqlParameter() { ParameterName = "@Text", SqlDbType = SqlDbType.NVarChar, Value = content },
                                          new SqlParameter() { ParameterName = "@Comment", SqlDbType = SqlDbType.NVarChar, Size = 255, Value = comment }
                                      };

                            int result = SqlHelper.ExecuteNonQuery(SqlHelper.ceterDB_Conn, CommandType.Text, sqlString, parameters);
                          }
                          catch (System.Exception ex) {
                            retVal = 1;
                            retMsg = "数据库查询出错, 错误代码: " + ex.Message + "...";
                          }
                        }
                        break;
                      case "0117": {
                          String eBayUserID = context.Request["ebayuserid"];
                          String orderID = context.Request["orderid"];

                          try {
                            String queryString = @"SELECT 1 FROM Blacklist b JOIN eBaySelling o ON b.UserID=o.UserID WHERE o.eBayUserID=@eBayUserID AND o.OrderID=@OrderID";
                            SqlParameter[] parameters = new SqlParameter[] {
                                          new SqlParameter() { ParameterName = "@eBayUserID", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = eBayUserID },
                                          new SqlParameter() { ParameterName = "@OrderID", SqlDbType = SqlDbType.NVarChar, Size = 30, Value = orderID }
                                      };
                            DataTable userIDTable = SqlHelper.ExecuteDataTable(SqlHelper.ceterDB_Conn, CommandType.Text, queryString, parameters);

                            if (userIDTable.Rows.Count > 0) {
                              retVal = 1010;
                              retMsg = "此ID已加入黑名单";
                            }
                            else {
                              String insertString = @"INSERT Blacklist(UserID) SELECT DISTINCT UserID FROM eBaySelling o WHERE o.eBayUserID=@eBayUserID AND o.OrderID=@OrderID";
                              parameters = new SqlParameter[] {
                                              new SqlParameter() { ParameterName = "@eBayUserID", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = eBayUserID },
                                              new SqlParameter() { ParameterName = "@OrderID", SqlDbType = SqlDbType.NVarChar, Size = 30, Value = orderID }
                                          };
                              SqlHelper.ExecuteNonQuery(SqlHelper.ceterDB_Conn, CommandType.Text, insertString, parameters);
                            }
                          }
                          catch (System.Exception ex) {
                            retVal = 1;
                            retMsg = "数据库查询出错, 错误代码: " + ex.Message.Replace("\r\n", " ") + "...";
                          }
                        }
                        break;
                      case "0118": {
                          String eBayUserID = context.Request["ebayuserid"];
                          String msgID = context.Request["msgid"];

                          try {
                            String queryString = @"SELECT 1 FROM Blacklist b JOIN eBayMessage m ON m.Sender=b.UserID WHERE m.eBayUserID=@eBayUserID AND m.MessageID=@MsgID";
                            SqlParameter[] parameters = new SqlParameter[] {
                                          new SqlParameter() { ParameterName = "@eBayUserID", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = eBayUserID },
                                          new SqlParameter() { ParameterName = "@MsgID", SqlDbType = SqlDbType.NVarChar, Size = 30, Value = msgID }
                                      };
                            DataTable userIDTable = SqlHelper.ExecuteDataTable(SqlHelper.ceterDB_Conn, CommandType.Text, queryString, parameters);

                            if (userIDTable.Rows.Count > 0) {
                              retVal = 1010;
                              retMsg = "此ID已加入黑名单";
                            }
                            else {
                              String insertString = @"INSERT Blacklist(UserID) SELECT DISTINCT Sender FROM eBayMessage m WHERE o.eBayUserID=@eBayUserID AND o.MessageID=@MsgID";
                              parameters = new SqlParameter[] {
                                              new SqlParameter() { ParameterName = "@eBayUserID", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = eBayUserID },
                                              new SqlParameter() { ParameterName = "@MsgID", SqlDbType = SqlDbType.NVarChar, Size = 30, Value = msgID }
                                          };
                              SqlHelper.ExecuteNonQuery(SqlHelper.ceterDB_Conn, CommandType.Text, insertString, parameters);
                            }
                          }
                          catch (System.Exception ex) {
                            retVal = 1;
                            retMsg = "数据库查询出错, 错误代码: " + ex.Message.Replace("\r\n", " ") + "...";
                          }
                        }
                        break;
                      case "0119": {
                          String eBayUserID = context.Request["eBayUserID"];
                          String feedbackID = context.Request["FeedbackID"];
                          String jsonReasons = context.Request["Reasons"];
                          String[] reasons = JsonConvert.DeserializeObject<List<String>>(jsonReasons).ToArray();

                          using (SqlConnection conn = new SqlConnection(SqlHelper.ceterDB_Conn)) {
                            try {
                              conn.Open();
                            }
                            catch (System.Exception ex) {
                              retVal = 1;
                              retMsg = "数据库查询出错, 错误代码: " + ex.Message.Replace("\r\n", " ") + "...";
                            }

                            using (SqlTransaction tran = conn.BeginTransaction()) {
                              String sqlString = "DELETE eBayFeedbackReason WHERE eBayUserID=@eBayUserID AND FeedbackID=@FeedbackID";
                              SqlParameter[] parameters = new SqlParameter[] {
                                              new SqlParameter() { ParameterName = "@eBayUserID", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = eBayUserID },
                                              new SqlParameter() { ParameterName = "@FeedbackID", SqlDbType = SqlDbType.VarChar, Size = 20, Value = feedbackID }
                                          };

                              SqlHelper.ExecuteNonQuery(tran, CommandType.Text, sqlString, parameters);

                              foreach (String reason in reasons) {
                                sqlString = "INSERT eBayFeedbackReason(eBayUserID,FeedbackID,Reason) VALUES(@eBayUserID,@FeedbackID,@Reason)";
                                parameters = new SqlParameter[] {
                                                  new SqlParameter() { ParameterName = "@eBayUserID", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = eBayUserID },
                                                  new SqlParameter() { ParameterName = "@FeedbackID", SqlDbType = SqlDbType.VarChar, Size = 20, Value = feedbackID },
                                                  new SqlParameter() { ParameterName = "@Reason", SqlDbType = SqlDbType.VarChar, Size = 20, Value = reason }
                                              };
                                SqlHelper.ExecuteNonQuery(tran, CommandType.Text, sqlString, parameters);
                              }
                              try {
                                tran.Commit();
                              }
                              catch (System.Exception ex) {
                                retVal = 1;
                                retMsg = "数据库查询出错, 错误代码: " + ex.Message.Replace("\r\n", " ") + "...";
                                tran.Rollback();
                              }
                            }
                          }
                        }
                        break;
                      case "0120": {
                          String type = context.Request["Type"];
                          String knowledgeID = context.Request["KnowledgeID"];
                          String productCode = context.Request["ProductCode"];
                          String question = context.Request["Question"];
                          String answer = context.Request["Answer"];
                          String jsonKeywords = context.Request["Keywords"];
                          String[] keywords = JsonConvert.DeserializeObject<List<String>>(jsonKeywords).ToArray();
                          String insertKeywords = String.Empty;

                          for (int i = 0; i < keywords.Length && i < 3; i++) {
                            insertKeywords += keywords[i];
                            insertKeywords += " ";
                          }

                          switch (type) {
                            case "i": {
                                using (SqlConnection conn = new SqlConnection(SqlHelper.ceterDB_Conn)) {
                                  try {
                                    conn.Open();
                                  }
                                  catch (System.Exception ex) {
                                    retVal = 1;
                                    retMsg = "数据库查询出错, 错误代码: " + ex.Message.Replace("\r\n", " ") + "...";
                                  }

                                  using (SqlTransaction tran = conn.BeginTransaction()) {
                                    String sqlInsertKnowledgeString = "INSERT KnowledgeBase(ProductCode,Question,Answer,Keywords,ImportDate,AUser) VALUES(@ProductCode,@Question,@Answer,@Keywords,@ImportDate,@AUser)\nSELECT @KnowledgeID=SCOPE_IDENTITY()";
                                    SqlParameter[] paramsInsertKnowledge = new SqlParameter[] {
                                                          new SqlParameter() { ParameterName = "@ProductCode", SqlDbType = SqlDbType.VarChar, Size = 80, Value = productCode },
                                                          new SqlParameter() { ParameterName = "@Question", SqlDbType = SqlDbType.NVarChar, Size = -1, Value = question },
                                                          new SqlParameter() { ParameterName = "@Answer", SqlDbType = SqlDbType.NVarChar, Size = -1, Value = answer },
                                                          new SqlParameter() { ParameterName = "@Keywords", SqlDbType = SqlDbType.NVarChar, Size = 255, Value = insertKeywords },
                                                          new SqlParameter() { ParameterName = "@ImportDate", SqlDbType = SqlDbType.DateTime, Value = DateTime.Now },
                                                          new SqlParameter() { ParameterName = "@KnowledgeID", SqlDbType = SqlDbType.Int, Direction = ParameterDirection.Output },
                                                          new SqlParameter() { ParameterName = "@AUser", SqlDbType = SqlDbType.NVarChar, Size = 255, Value = context.Session["UserId"] }
                                                      };
                                    SqlHelper.ExecuteNonQuery(tran, CommandType.Text, sqlInsertKnowledgeString, paramsInsertKnowledge);

                                    foreach (String keyword in keywords) {
                                      String sqlInsertKeywordString = "INSERT KnowledgeKeyword(KnowledgeID,Keyword) VALUES(@KnowledgeID, @Keyword)";

                                      SqlParameter[] paramsInsertKeyword = new SqlParameter[] {
                                                              new SqlParameter() { ParameterName = "@KnowledgeID", SqlDbType = SqlDbType.Int, Value = paramsInsertKnowledge[5].Value },
                                                              new SqlParameter() { ParameterName = "@Keyword", SqlDbType = SqlDbType.NVarChar, Size = 255, Value = keyword }
                                                          };

                                      SqlHelper.ExecuteNonQuery(tran, CommandType.Text, sqlInsertKeywordString, paramsInsertKeyword);
                                    }

                                    try {
                                      tran.Commit();
                                    }
                                    catch (System.Exception ex) {
                                      retVal = 1;
                                      retMsg = "数据库查询出错, 错误代码: " + ex.Message.Replace("\r\n", " ") + "...";
                                      tran.Rollback();
                                    }
                                  }
                                }
                              }
                              break;
                            case "u": {
                                using (SqlConnection conn = new SqlConnection(SqlHelper.ceterDB_Conn)) {
                                  try {
                                    conn.Open();
                                  }
                                  catch (System.Exception ex) {
                                    retVal = 1;
                                    retMsg = "数据库查询出错, 错误代码: " + ex.Message.Replace("\r\n", " ") + "...";
                                  }

                                  using (SqlTransaction tran = conn.BeginTransaction()) {
                                    String sqlUpdateKnowledgeString = "UPDATE KnowledgeBase SET ProductCode=@ProductCode,Question=@Question,Answer=@Answer,Keywords=@Keywords,AUser=@AUser WHERE ID=@KnowledgeID";
                                    SqlParameter[] paramsUpdateKnowledge = new SqlParameter[] {
                                                          new SqlParameter() { ParameterName = "@ProductCode", SqlDbType = SqlDbType.VarChar, Size = 80, Value = productCode },
                                                          new SqlParameter() { ParameterName = "@Question", SqlDbType = SqlDbType.NVarChar, Size = -1, Value = question },
                                                          new SqlParameter() { ParameterName = "@Answer", SqlDbType = SqlDbType.NVarChar, Size = -1, Value = answer },
                                                          new SqlParameter() { ParameterName = "@Keywords", SqlDbType = SqlDbType.NVarChar, Size = 255, Value = insertKeywords },
                                                          new SqlParameter() { ParameterName = "@KnowledgeID", SqlDbType = SqlDbType.Int, Value = knowledgeID },
                                                          new SqlParameter() { ParameterName = "@AUser", SqlDbType = SqlDbType.NVarChar, Size = 255, Value = context.Session["UserId"] }
                                                      };
                                    SqlHelper.ExecuteNonQuery(tran, CommandType.Text, sqlUpdateKnowledgeString, paramsUpdateKnowledge);

                                    String sqlDeleteKeywordString = "DELETE KnowledgeKeyword WHERE KnowledgeID=@KnowledgeID";
                                    SqlParameter[] paramsDeleteKeyword = new SqlParameter[] {
                                                          new SqlParameter() { ParameterName = "@KnowledgeID", SqlDbType = SqlDbType.Int, Value = knowledgeID }
                                                      };

                                    SqlHelper.ExecuteNonQuery(tran, CommandType.Text, sqlDeleteKeywordString, paramsDeleteKeyword);
                                    foreach (String keyword in keywords) {
                                      String sqlInsertKeywordString = "INSERT KnowledgeKeyword(KnowledgeID,Keyword) VALUES(@KnowledgeID, @Keyword)";

                                      SqlParameter[] paramsInsertKeyword = new SqlParameter[] {
                                                              new SqlParameter() { ParameterName = "@KnowledgeID", SqlDbType = SqlDbType.Int, Value = knowledgeID },
                                                              new SqlParameter() { ParameterName = "@Keyword", SqlDbType = SqlDbType.NVarChar, Size = 255, Value = keyword }
                                                          };

                                      SqlHelper.ExecuteNonQuery(tran, CommandType.Text, sqlInsertKeywordString, paramsInsertKeyword);
                                    }

                                    try {
                                      tran.Commit();
                                    }
                                    catch (System.Exception ex) {
                                      retVal = 1;
                                      retMsg = "数据库查询出错, 错误代码: " + ex.Message.Replace("\r\n", " ") + "...";
                                      tran.Rollback();
                                    }
                                  }
                                }
                              }
                              break;
                          }
                        }
                        break;
                      case "0121": {
                          String knowledgeID = context.Request["KnowledgeID"];
                          String answer = context.Request["Answer"];

                          String sqlString = "UPDATE KnowledgeBase SET Answer=@Answer WHERE ID=@KnowledgeID";
                          SqlParameter[] parameters = new SqlParameter[] {
                                  new SqlParameter() { ParameterName = "@KnowledgeID", SqlDbType = SqlDbType.Int, Value = knowledgeID },
                                  new SqlParameter() { ParameterName = "@Answer", SqlDbType = SqlDbType.NVarChar, Size = -1, Value = answer }
                                };

                          try {
                            SqlHelper.ExecuteNonQuery(SqlHelper.ceterDB_Conn, CommandType.Text, sqlString, parameters);
                          }
                          catch (System.Exception ex) {
                            retVal = 1;
                            retMsg = "数据库查询出错, 错误代码: " + ex.Message.Replace("\r\n", " ") + "...";
                          }
                        }
                        break;
                      // update feedback track level
                      case "tracklevel": {
                          String eBayUserID = context.Request["ebayuserid"];
                          String feedbackID = context.Request["feedbackid"];
                          String trackLevel = context.Request["tracklevel"];
                          ebay.BLL.FeedbackManage manage = new ebay.BLL.FeedbackManage();

                          manage.UpdateFeedbackTrackLevel(eBayUserID, feedbackID, trackLevel);
                        }
                        break;
                    }
                  }
                  break;
                #endregion // update
              case "i":
                  {
                      switch(id)
                      {
                          // Insert message to send
                        case "0001":
                            {
                                String eBayUserID = context.Request["ebayuserid"];
                                String msgID = context.Request["originalmsgid"];
                                String msgIDEx = context.Request["externalmessageid"];
                                String recipientID = context.Request["recipientid"];
                                String subject = context.Request["Subject"];
                                String body = context.Request["body"];
                                String orderID = context.Request["orderid"];
                                String folderID = context.Request["folderid"];
                                String sqlString = @"INSERT OigMessage(eBayUserID,OriginalMsgID,ParentMsgID,RecipientID,Subject,Body,FolderID,ImportDate)
  VALUES(@eBayUserID,@OriginalMsgID,@ParentMsgID,@RecipientID,@Subject,@Body,@FolderID,@ImportDate)
UPDATE OigMessage SET OrderID=@OrderID,OrderSalesRecordNumber=o.OrderSalesRecordNumber FROM eBaySelling o WHERE o.OrderID=@OrderID";
                                SqlParameter[] parameters = new SqlParameter[] {
                                    new SqlParameter() { ParameterName = "@eBayUserID", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = eBayUserID },
                                    new SqlParameter() { ParameterName = "@OriginalMsgID", SqlDbType = SqlDbType.VarChar, Size = 25, Value = msgID },
                                    new SqlParameter() { ParameterName = "@ParentMsgID", SqlDbType = SqlDbType.VarChar, Size = 25, Value = msgIDEx },
                                    new SqlParameter() { ParameterName = "@RecipientID", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = recipientID },
                                    new SqlParameter() { ParameterName = "@Subject", SqlDbType = SqlDbType.NVarChar, Size = 255, Value = subject },
                                    new SqlParameter() { ParameterName = "@Body", SqlDbType = SqlDbType.Text, Value = body },
                                    new SqlParameter() { ParameterName = "@FolderID", SqlDbType = SqlDbType.Int, Value = folderID },
                                    new SqlParameter() { ParameterName = "@OrderID", SqlDbType = SqlDbType.VarChar, Size = 30, Value = orderID },
                                    new SqlParameter() { ParameterName = "ImportDate", SqlDbType = SqlDbType.DateTime, Value = DateTime.Now }
                                };
                  
                                try {
                                    int result = SqlHelper.ExecuteNonQuery(SqlHelper.ceterDB_Conn, CommandType.Text, sqlString, parameters);
                                }
                                catch(System.Exception ex) {
                                    retVal = 1;
                                    retMsg = "数据库执行出错, 错误代码: " + ex.Message.Replace("\r\n", " ") + "...";
                                }
                            }
                            break;
                            // Insert message to save
                        case "0002":
                            {
                                String eBayUserID = context.Request["ebayuserid"];
                                String msgID = context.Request["messageid"];
                                String msgIDEx = context.Request["externalmessageid"];
                                String recipientID = context.Request["recipientid"];
                                String body = context.Request["body"];
                                String sqlString = "INSERT OigMessage(eBayUserID,OriginalMsgID,ParentMsgID,RecipientID,Body,FolderID) VALUES(@eBayUserID,@OriginalMsgID,@ParentMsgID,@RecipientID,@Body,@FolderID)";
                                SqlParameter[] parameters = new SqlParameter[] {
                                    new SqlParameter() { ParameterName = "@eBayUserID", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = eBayUserID },
                                    new SqlParameter() { ParameterName = "@OriginalMsgID", SqlDbType = SqlDbType.VarChar, Size = 25, Value = msgID },
                                    new SqlParameter() { ParameterName = "@ParentMsgID", SqlDbType = SqlDbType.VarChar, Size = 25, Value = msgIDEx },
                                    new SqlParameter() { ParameterName = "@RecipientID", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = recipientID },
                                    new SqlParameter() { ParameterName = "@Body", SqlDbType = SqlDbType.Text, Value = body },
                                    new SqlParameter() { ParameterName = "@FolderID", SqlDbType = SqlDbType.Int, Value = 8448 }
                                };

                                try
                                {
                                    int result = SqlHelper.ExecuteNonQuery(SqlHelper.ceterDB_Conn, CommandType.Text, sqlString, parameters);
                                }
                                catch (System.Exception ex)
                                {
                                    retVal = 1;
                                    retMsg = "数据库执行出错, 错误代码: " + ex.Message.Replace("\r\n", " ") + "...";
                                }
                            }
                            break;
                        case "0003":
                            {
                                String eBayUserID = context.Request["ebayuserid"];
                                String feedbackID = context.Request["feedbackid"];
                                String msg = context.Request["msg"];

                            }
                            break;
                          case "0004":
                              {
                                  String name = context.Request["name"];
                                  String content = context.Request["content"];
                                  String comment = context.Request["comment"];
                                  String sqlString = @"INSERT MessageTemplate(Name,Text,Comment) VALUES(@Name,@Text,@Comment)";
                                  SqlParameter[] parameters = new SqlParameter[] {
                                      new SqlParameter() { ParameterName = "@Name", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = name },
                                      new SqlParameter() { ParameterName = "@Text", SqlDbType = SqlDbType.NVarChar, Value = content },
                                      new SqlParameter() { ParameterName = "@Comment", SqlDbType = SqlDbType.NVarChar, Size = 255, Value = comment }
                                  };
                                  try {
                                      SqlHelper.ExecuteNonQuery(SqlHelper.ceterDB_Conn, CommandType.Text, sqlString, parameters);
                                  }
                                  catch (System.Exception ex)
                                  {
                                      retVal = 1;
                                      retMsg = "数据库执行出错, 错误代码: " + ex.Message.Replace("\r\n", " ") + "...";
                                  }
                              }
                            break;
                      }
                  }
                  break;
              case "d":
                  {
                      switch (id)
                      {
                        case "0101":
                            {
                                String eBayUserID = context.Request["ebayuserid"];
                                String messageID = context.Request["messageid"];

                                try
                                {
                                    String sqlString = "DELETE eBayMessageWHERE eBayUserID=@eBayUserID AND MessageID=@MessageID";
                                    SqlParameter[] parameters = new SqlParameter[] {
                                        new SqlParameter() { ParameterName = "@eBayUserID", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = eBayUserID },
                                        new SqlParameter() { ParameterName = "@MessageID", SqlDbType = SqlDbType.VarChar, Size = 25, Value = messageID }
                                    };

                                    int result = SqlHelper.ExecuteNonQuery(SqlHelper.ceterDB_Conn, CommandType.Text, sqlString, parameters);
                                }
                                catch (System.Exception ex)
                                {
                                    retVal = 1;
                                    retMsg = "数据库执行出错, 错误代码: " + ex.Message.Replace("\r\n", " ") + "...";
                                }
                            }
                            break;
                            // delete ebay message
                        case "0102":
                            {
                                String eBayUserID = context.Request["ebayuserid"];
                                String msgID = context.Request["messageid"];
                                MessageService msg = new MessageService();

                                try
                                {
                                    msg.DeleteMessage(eBayUserID, msgID);
                                }
                                catch (System.Exception ex)
                                {
                                    retVal = 1;
                                    retMsg = "数据库执行出错, 错误代码: " + ex.Message.Replace("\r\n", " ") + "...";
                                }
                            }
                            break;
                        // delete response
                        case "0103":
                            {
                                String eBayUserID = context.Request["ebayuserid"];
                                String msgID = context.Request["messageid"];
                                ebay.Dal.Response response = new ebay.Dal.Response();

                                try
                                {
                                    response.DeleteResponse(eBayUserID, msgID);
                                }
                                catch (System.Exception ex)
                                {
                                    retVal = 1;
                                    retMsg = "数据库执行出错, 错误代码: " + ex.Message.Replace("\r\n", " ") + "...";
                                }
                            }
                            break;
                          case "0104":
                              {
                                  String templateID = context.Request["templateid"];
                                  String sqlString = "DELETE MessageTemplate WHERE ID=@ID";
                                  SqlParameter[] parameters = new SqlParameter[] {
                                      new SqlParameter() { ParameterName = "@ID", SqlDbType = SqlDbType.Int, Value = templateID }
                                  };
                                  try {
                                      SqlHelper.ExecuteNonQuery(SqlHelper.ceterDB_Conn, CommandType.Text, sqlString, parameters);
                                  }
                                  catch(System.Exception ex)
                                  {
                                      retVal = 1;
                                      retMsg = "数据库执行出错, 错误代码: " + ex.Message.Replace("\r\n", " ") + "...";
                                  }
                              }
                              break;
                          case "0105":
                              {
                                  String knowledgeID = context.Request["knowledgeid"];
                                  String sqlString = "DELETE KnowledgeBase WHERE ID=@ID";
                                  SqlParameter[] parameters = new SqlParameter[] {
                                      new SqlParameter() { ParameterName = "@ID", SqlDbType = SqlDbType.Int, Value = knowledgeID }
                                  };
                                  try {
                                      SqlHelper.ExecuteNonQuery(SqlHelper.ceterDB_Conn, CommandType.Text, sqlString, parameters);
                                  }
                                  catch (System.Exception ex)
                                  {
                                      retVal = 1;
                                      retMsg = "数据库执行出错, 错误代码: " + ex.Message.Replace("\r\n", " ") + "...";
                                  }
                              }
                              break;
                      }
                  }
                  break;
              default:
                  break;
            }
        }
        //String json = "{\"total\":2,\"rows\":[{ \"Flag\": \"red\", \"From\": \"lichunhua\", \"Title\": \"this is a test\", \"OrderId\": \"B100000\" },{ \"Flag\": \"red\", \"From\": \"lichunhua\", \"Title\": \"this is a test\", \"OrderId\": \"B100000\" }]}";
        String json = String.Format(
                                    "{{ \"retVal\": {0}, \"retMsg\": \"{1}\", \"retSrc\": {2} }}",
                                    retVal,
                                    retMsg,
                                    String.IsNullOrEmpty(retSrc) ? "\"\"" : retSrc
                                    );
        context.Response.Write(json);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}
